<?php

require_once '../mysqlConnect.php';


class QueryController {

	function __autoload($class_name) {
		include $class_name . '.php';
	}
	
	function query($basic_patient_info, $physical_symptom_info = null, $neuropsychological_symptom_info = null, $psychological_test_info = null, $diagnostic_test_info = null, $icd_info = null, $prescription_info = null) {
		
		$conn = GetPDOConnection();
		$result = 'success';
		
		$basic_patient_info = $this->queryPatientInformation($basic_patient_info, $conn);
	
		if (count($basic_patient_info) < 1) {
			
			return array(
								'result' => 'error',
								'message' => 'No patients match filters from Basic Information'
			);
		} else {
		
			return array(
						'result' => 'success',
						'message' => array(
			
								'patient_info' => $basic_patient_info
								
								)
					);
		}
	}
	
	
	function queryPatientInformation ($postVariables, $conn) {
		
		$formAnswers = json_decode($postVariables);
		
		$basic_patient_info = array();
		foreach ($formAnswers as $stdClassObj)
			$basic_patient_info[$stdClassObj->id] = $stdClassObj;
	
		$sqlFindPatients = "SELECT * " . 
										"FROM Patient " .  
											"WHERE 1=1 ";
		
		$dd1 = urldecode($basic_patient_info['DD1']->value);
		$dd2 = urldecode($basic_patient_info['DD2']->value);
		$sqlFindPatients .= (( $dd1 != "Leave blank for no value" && trim($dd1) != "") && ($dd2 != "Leave blank for no value" && trim($dd2) != ""))
									? "AND DateDiagnosed BETWEEN '" . min($dd1, $dd2) . "' AND '" . max($dd1, $dd2) . "' " : null;
		
		$dob1 = urldecode($basic_patient_info['DOB1']->value);
		$dob2 = urldecode($basic_patient_info['DOB2']->value);
		$sqlFindPatients .= ($dob1 != "Leave blank for no value" && trim($dob1) != "") && ($dob2 != "Leave blank for no value" && trim($dob2) != "")
									? "AND DOB BETWEEN '" . min($dob1, $dob2) . "' AND '" . max($dob1, $dob2) . "' " : null;
		

		$sqlFindPatients .= $basic_patient_info['selectGender']->value != "E" ? "AND Gender = '" . $basic_patient_info['selectGender']->value . "' " : null;
		
		$sqlFindPatients .= $basic_patient_info['selectRelationship']->value != "E" ? "AND RelationshipStatus = '" . $basic_patient_info['selectRelationship']->value . "' " : null;
		
		$sqlFindPatients .= $basic_patient_info["selectOccupation"]->value != "E" ? "AND OccupationStatus = '" . $basic_patient_info['selectOccupation']->value . "' " : null;
		
		$sqlFindPatients .= $basic_patient_info["selectTickWorkEnv"]->value != "E" ? "AND WorkHabitat = '" . $basic_patient_info['selectTickWorkEnv']->value . "' " : null;
		
		$sqlFindPatients .= $basic_patient_info["selectTickHuntEnv"]->value != "E" ? "AND HuntHabitat = '" . $basic_patient_info['selectTickHuntEnv']->value . "' " : null;
		
		$sqlFindPatients .= $basic_patient_info["selectTickPlayEnv"]->value != "E" ? "AND PlayHabitat = '" . $basic_patient_info['selectTickPlayEnv']->value . "' " : null;
		
		$sqlFindPatients .= $basic_patient_info["selectTickGardenEnv"]->value != "E" ? "AND GardenHabitat = '" . $basic_patient_info['selectTickGardenEnv']->value . "' " : null;
		
		$sqlFindPatients .= $basic_patient_info["selectTickBite"]->value != "E" ? "AND TickBite = '" . $basic_patient_info['selectTickBite']->value . "' " : null;
		
		$sqlFindPatients .= $basic_patient_info["selectRash"]->value != "E" ? "AND Rash = '" . $basic_patient_info['selectRash']->value . "' " : null;
		
		$sqlFindPatients .= " ORDER BY PatID";
		
		$STH = $conn->prepare($sqlFindPatients);
		$STH->setFetchMode(PDO::FETCH_ASSOC);
		$STH->execute();
		$patientsArray = $STH->fetchAll();
		return $patientsArray;
	}
	
	function queryPhysicalSymptoms($postVariables, $conn) {
		$folder = getcwd();
		$fh = fopen($folder . '/text.txt', 'w+');
	}
}